<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of manage_users_controllers
 *
 * @author chandu
 */
include "base.php";
include CONFIG_PATH . 'errors.php';

class manage_users_controller extends baseController {

    public $manage_users;
    public $reslt;
    private $module_check;
    public $users_display;
    public $permission;
    public $data;
    public $update;
    public $mod;
    public $errors;
	public $m_users;

    public function __construct() {
        parent::__construct();
        if (!$_SESSION) {
            echo '<script>window.location.assign("index.php")</script>';
        }
      //  $this->mod = new model();

        $this->errors = new errors();
        $this->manage_users = new manage_users_model();
        $this->permission = $this->manage_users->get_permissions($this->tables->table2, "Users");
        // This is the code for navbar 
		$this->m_users = "active";
    }

    public function invoke() {
        //This  code is to delete users if user have delete permissions
        if (isset($_POST['delete'])) {
//
            if (($_SESSION['Del'] == "Yes") or ($_SESSION['admin'] == 1)) {
                $this->reslt = $this->manage_users->delete($this->tables->table2, $_POST['chkDel'], "p_id");
                if ($this->reslt) {

                    $msg = $this->errors->delete_successful;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                                 window.location.reload();
                      </SCRIPT>");
                } else {

                    $msg = $this->errors->default_failed;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                }
            } else {
                //if user doesnt have delete permission then this code will run
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="manage_users.php";
                        </script>';
            }
        }
//This  code is to get users details
        if (isset($_GET['details'])) {

            $this->data = $_GET['details'];
			
			$_SESSION['suserid'] = $this->data;
            if (($_SESSION['Edit'] == "Yes") or ($_SESSION['admin'] == 1)) {
                $this->reslt = $this->manage_users->details($this->tables->table2, $this->data, "userid");
                $this->show("users/user_details");
                exit();
            } else {
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="manage_users.php";
                        </script>';
            }
        }
        //This  code is to edit module if user have edit permissions
        if (isset($_GET['edit'])) {
//
            $this->data = $_GET['edit'];
            if (($_SESSION['Edit'] == "Yes") or ($_SESSION['admin'] == 1)) {
                if ($_POST) {

                    $this->update = $this->manage_users->update($this->tables->table2, $this->data, "p_id", $_POST);

                    if ($this->update == "Success") {
                        $msg = $this->errors->update_successful;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    } else if ($this->update == 'Failed') {

                        $msg = $this->errors->default_failed;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    }
                }
            } else {
                //if user doesnt have edit permission then this code will run
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="manage_users.php";
                        </script>';
            }
            $this->reslt = $this->manage_users->details($this->tables->table2, $this->data, "p_id");
            $this->show("users/user_module_details");
            exit();
        }
          //This  code is to add module if user have add permissions
        if (isset($_GET['add'])) {
		if (($_GET['add']) !== "0" or ($_GET['add']) !== "") {
             $this->data = $_GET['add'];
            if (($_SESSION['Addd'] == "Yes") or ($_SESSION['admin'] == 1)) {
                if ($_POST) {
                    $this->reslt = $this->manage_users->add_module($this->tables->table2, $this->data);
                    if ($this->reslt == "Success") {
                        $msg = $this->errors->module_inserted_successfully;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    } else if ($this->reslt == "Failed") {
                        $msg = $this->errors->default_failed;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    } else {
                        $msg = $this->errors->module_already_exists;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    }
                }
                $this->modules = $this->manage_users->get_modules($this->tables->table4);
                $this->show("users/add_module");
            } else {
                 //if user doesnt have add permission then this code will run
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="manage_users.php";
                        </script>';
            }
        } else {
             // this code will run when this page is requested with out login
		$msg = $this->errors->invalidrequest;
		    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
		}
		} else {
                     //this code is to delete module if user have delete permissions
            if ($_POST) {
                if (($_SESSION['Del'] == "Yes") or ($_SESSION['admin'] == 1)) {
                    $this->reslt = $this->manage_users->delete($this->tables->table1, $_POST['chkDel'], "userid");
                    if ($this->reslt) {

                     $msg = $this->errors->delete_successful;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                                 window.location.reload();
                      </SCRIPT>");
                    } else {

                        $msg = $this->errors->default_failed;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');                         
                      </SCRIPT>");
                    }
                } else {
                     //if user doesnt have delete permission then this code will run
                       $msg = $this->errors->access_denied;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="manage_users_controller.php";
                        </script>';
                }
            }

            $this->users_display = $this->manage_users->users_display($this->tables->table1);
            $this->show("users/users");
        }
    }

}

$manage = new manage_users_controller();
$manage->invoke();
?>